package com.mtr.databoard.application.middlewares;


import com.alibaba.fastjson.JSON;
import com.mtr.databoard.application.EntityResultDto;
import com.mtr.databoard.application.PagedResultDto;
import com.mtr.databoard.application.middlewares.dto.CreateMiddlewareInput;
import com.mtr.databoard.application.middlewares.dto.ExecuteMiddlewareInput;
import com.mtr.databoard.application.middlewares.dto.UpdateMiddlewareInput;
import com.mtr.databoard.common.linqlist.LinqList;
import com.mtr.databoard.domain.middlewares.Middleware;
import com.mtr.databoard.domain.middlewares.IMiddlewareRepository;
import com.mtr.databoard.domain.middlewares.Middleware;
import com.mtr.databoard.domain.middlewares.MiddlewareManager;
import com.mtr.databoard.domain.resources.ResourceManager;
import org.bson.types.Binary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("middleware")
public class MiddlewareApplication {

    private IMiddlewareRepository middlewareRepository;

    private MiddlewareManager middlewareManager;

    private ResourceManager resourceManager;

    public MiddlewareApplication(@Autowired IMiddlewareRepository middlewareRepository,@Autowired MiddlewareManager middlewareManager,@Autowired ResourceManager resourceManager) {
        this.middlewareRepository = middlewareRepository;
        this.middlewareManager = middlewareManager;
        this.resourceManager = resourceManager;
    }

    @GetMapping("/getall")
    @ResponseBody
    public EntityResultDto<PagedResultDto<Middleware>> getAllMiddlewares() {
        List<Middleware> middlewares = middlewareRepository.findAll();
        return EntityResultDto.newSuccessResult(new PagedResultDto<>(middlewares.size(), middlewares));
    }

    @GetMapping("/get/{id}")
    @ResponseBody
    public EntityResultDto<Middleware> getMiddleware(@PathVariable UUID id) {
        if (!middlewareRepository.existsById(id)) return EntityResultDto.newErrorResult(null);
        Middleware middleware = middlewareRepository.findById(id).get();
        return EntityResultDto.newSuccessResult(middleware);
    }

    @DeleteMapping("/delete/{id}")
    @ResponseBody
    public EntityResultDto<Middleware> deleteMiddleware(@PathVariable UUID id) {
        if (!middlewareRepository.existsById(id)) return EntityResultDto.newErrorResult(null);

        Middleware middleware = middlewareRepository.findById(id).get();
        middlewareManager.delete(id);
        return EntityResultDto.newSuccessResult(middleware);
    }

    @PostMapping("/copy/{id}")
    @ResponseBody
    public EntityResultDto<Middleware> copyMiddleware(@PathVariable UUID id) throws CloneNotSupportedException, IOException {
        if (!middlewareRepository.existsById(id)) return EntityResultDto.newErrorResult(null);

        Middleware middleware = middlewareManager.copy(id);
        return EntityResultDto.newSuccessResult(middleware);
    }



    @PostMapping("/create")
    @ResponseBody
    public EntityResultDto<Middleware> createMiddleware(@RequestBody CreateMiddlewareInput input) throws IOException {
        Middleware middleware = middlewareManager.create(input.getCover(),input.getName(),input.getDescription(),input.getMiddleware(),input.getMiddlewareEntry(),input.getUpdateFrequency(),input.getParameters(),input.getIsPush());
        return EntityResultDto.newSuccessResult(middleware);
    }

    @PutMapping("/update")
    @ResponseBody
    public EntityResultDto<Middleware> updateMiddleware(@RequestBody UpdateMiddlewareInput input) throws IOException {
        if (!middlewareRepository.existsById(input.getId()))
            return EntityResultDto.newErrorResult(null);
        middlewareManager.update(input.getId(),input.getCover(),input.getName(),input.getDescription(),input.getMiddleware(),input.getMiddlewareEntry(),input.getUpdateFrequency(),input.getParameters(),input.getIsPush());


        return EntityResultDto.newSuccessResult(middlewareRepository.findById(input.getId()).get());
    }

    @PostMapping("/execute")
    @ResponseBody
    public EntityResultDto<String> executeMiddleware(@RequestBody ExecuteMiddlewareInput input) throws IOException {
        if (!middlewareRepository.existsById(input.getId()))
            return EntityResultDto.newErrorResult(null);
        var obj =middlewareManager.execute(input.getId(),input.getParameters());
        var value =JSON.toJSONString(obj);
        return EntityResultDto.newSuccessResult(value);
    }

}
